package cn.w.outdoor.lib.common;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import cn.w.outdoor.lib.media.MediaConfig;

public class CameraTools {

    private final static String TAG = "CameraTools";

    private static final CameraTools ourInstance = new CameraTools();

    public static CameraTools getInstance() {
        return ourInstance;
    }

    private CameraTools() {
    }

    private int cameraId; // 要获取数据的摄像头的ID信息
    private Camera camera; // 要获取数据的摄像头

    private final AtomicBoolean control = new AtomicBoolean(false);

    public void initCamera(SurfaceView surfaceView) {
        synchronized (control) {
            if (!control.get()) {
                Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                int cameraCount = Camera.getNumberOfCameras();
                log("获取到摄像头的数量为：" + cameraCount);

                if (cameraCount == 0) {
                    log("没有获取到当前设备的摄像头信息，初始化失败");
                    return;
                }

                try {
                    this.camera = Camera.open(0);
                } catch (Exception e) {
                    log("打开摄像头失败");
                    return;
                }

                Camera.Parameters parameters = this.camera.getParameters();
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); // 无闪光灯
                parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);

                parameters.setPreviewFormat(ImageFormat.NV21); // 默认为NV21，还可以设定为YV12
                parameters.setPreviewSize(MediaConfig.VIDEOWIDTH, MediaConfig.VIDEOHEIGHT);
//                this.camera.setDisplayOrientation(180);
                this.camera.setParameters(parameters);

                try {
                    this.camera.setPreviewDisplay(surfaceView.getHolder());
                } catch (IOException e) {
                    log("设置 SurfaceView 失败");
                    e.printStackTrace();
                }

                control.set(true);
            }
        }
    }

    private final AtomicBoolean previewControl = new AtomicBoolean(false);

    public void startCamera(Camera.PreviewCallback previewCallback) {
        synchronized (previewControl) {
            if (!previewControl.get()) {
                this.camera.setPreviewCallback(previewCallback);
                this.camera.startPreview();
                previewControl.set(true);
            }
        }
    }

    public void stopCamera() {
        synchronized (previewControl) {
            if (previewControl.get()) {
                this.camera.stopPreview();
                this.camera.setPreviewCallback(null);
                previewControl.set(false);
            }
        }
    }

    public void releaseCamera() {
        synchronized (control) {
            if (control.get()) {
                stopCamera();
                this.camera.release();
                control.set(false);
            }
        }
    }

    private void log(String log) {
        Log.e(TAG, log);
    }

}
